knitr::opts_chunk$set(fig.width=5, fig.height=5) 
knitr::opts_chunk$set(tidy.opts = list(width.cutoff = 60), tidy = TRUE)

rm(list=ls())

options(digits=2)

pkgs <- c("dplyr", "ggplot2", "stargazer",  "tidyverse", "extrafont", "scales", "plyr", "caret", "estimatr", "ggeffects", "formatR","texreg")
# A function to load all above packages. Install if they have not been installed.
usePackage <- function(p){
  for (pkg in p){
    if (!is.element(pkg, installed.packages()[,1]))
      install.packages(pkg, dep = TRUE, repos = "https://cloud.r-project.org/")
    require(pkg, character.only = TRUE)
  }
}
usePackage(pkgs)

## TO RUN PLACEBO-LIKE GROUP REPLACE WITH anonymousdatanotreat.csv ##
m <- as.data.frame(read.csv("anonymousdata.csv"))
### US Respondent
m$USRespondent <- as.factor(ifelse(m$Country == 187, 1, 0))

### Demographics

### 3 = Extremely liberal, -3 = Extremely conservative

m$Ideology <- ifelse(m$Ideology == 0, m$IdeologyM, m$Ideology)

m$Liberal <- ifelse(m$Ideology > 0 , 1, 0)
m$Conservative <- ifelse(m$Ideology < 0 , 1, 0)

m$Ideology2 <- as.factor(ifelse(m$Ideology == 0, m$IdeologyM, m$Ideology))
m$Ideology2 <- relevel(m$Ideology2, ref = "0")


### 3 = Strong Democrat, -3 = Strong Republican, 99 = Unsure

m$Party <- ifelse(m$PartyID == 99, 0, m$PartyID)

m$Party2 <- as.factor(ifelse(m$PartyID == 99, 0, m$PartyID))
m$Party2 <- relevel(m$Party2, ref = "0")


m$Democrat <- as.factor(ifelse(m$Party > 0, 1, 0 ))
m$Republican <- as.factor(ifelse(m$Party <0 , 1, 0))


## Dem = 1, Rep = -1, Independent = 0
m$PID3 <- as.factor(ifelse(m$Democrat == 1, 1, ifelse(m$Republican == 1, -1, 0)))

### 1 = female
m$Female <- m$Sex

## Race
m$White <- ifelse(m$Race == 1, 1, 0)
m$Black <- ifelse(m$Race == 2, 1, 0)
m$Hispanic <- ifelse(m$Race == 3, 1, 0)
m$Asian <- ifelse(m$Race == 4, 1, 0)

# Education (1 = Some HS, 2 = HS, 3 = Some College, 4 = BA, 5 = Grad Degree)
#summary(m$Education)

# Age
m$age <- 2021 - m$Age


### Treatment Groups

## Blindspot for Dems - Biden restricting Cuban Refugees
m$CubanTreatment <- ifelse(m$Version == "A" , 1, 0)

## Blindspot for Reps - Gov. talking about vaccine misinformation 
m$VaccineTreatment <- ifelse(m$Version == "A" , 1, 0)

## Blindspot for Dems - Biden finds lab leak credible 
m$WIVTreatment <- ifelse(m$Version == "B" , 1, 0)

## Blindspot for Reps - Putin's plot to put Trump in WH 
m$RussiaTreatment <- ifelse(m$Version == "B" , 1, 0)

### Fix mistaken label from Qualtrics
m$R_Russia_Pos <- m$R_Russa_Pos
m$Russia_Pos <- m$Russa_Pos


m$VersionDummy <- ifelse(m$Version == "A", 1, 0)


m$NonUS <- ifelse(m$USRespondent == 0, 1, 0)
m$Democrat <- ifelse(m$Democrat == 1 & m$USRespondent == 1, 1, 0)
m$Republican <- ifelse(m$Republican == 1 & m$USRespondent == 1 ,1 , 0)
m$Independent <- ifelse(m$Democrat == 0 & m$Republican == 0 & m$NonUS == 0 , 1, 0)

BalanceCheck <- lm(VersionDummy ~ Democrat + Republican + NonUS  + White + Black + Hispanic + age + Female, data = m)

m$BlindspotEmpathetic <- m$BlindspotQA_Empathetic
m$BlindspotChangeMind <- m$BlindspotQA_ChangeMind
m$BlindspotOthersBiases <- m$BlindspotQA_OtherBias
m$BlindspotMyBias <- m$BlindspotQA_MyBias
m$BlindspotViewsDisagree <- m$BlindspotQA_ViewsDiss



m2 <- subset(m, m$OpenedEmail == 1)


### DEMOGRAPHICS OF SAMPLE AND THOSE WHO OPENED EMAIL

stargazer(m, header = F, keep =c("NonUS" ,"Democrat", "Republican", "Independent", "Ideology2", "Female", "White", "Black", "Asian", "Hispanic", "Education", "age"), digits = 2, title = "Table S1: Descriptive statistics for respondents")

stargazer(m2, header = F, keep =c("NonUS" ,"Democrat", "Republican", "Independent", "Ideology2", "Female", "White", "Black", "Asian", "Hispanic", "Education", "age"), digits = 2, title = "Table S2: Descriptive statistics for respondents who had opened the newsletter at the time the second wave of the survey launched")

stargazer(m, header = F, keep =c("BlindspotMyBias" ,"BlindspotOthersBiases", "BlindspotViewsDisagree", "BlindspotEmpathetic", "BlindspotChangeMind"), digits = 2, title = "Table S3: Respondent's evaluation of Blindspot Report")

stargazer(BalanceCheck, digits = 2, header = F,title = "Table S4: Balance check")


### CODING VARIABLES

#Issue Importance


m$PrePostCubanImportance <- m$R_Cuban_Imp - m$Cuban_Imp
m$PrePostVaccineImportance <- m$R_Vaccine_Imp - m$Vaccine_Imp
m$PrePostWIVImportance <- m$R_WIV_Imp - m$WIV_Imp 
m$PrePostRussiaImportance <- m$R_Russia_Imp - m$Russia_Imp


## Models
ImpCubanModel <- lm(m$PrePostCubanImportance ~ m$CubanTreatment)
ImpVaccineModel <- lm(m$PrePostVaccineImportance ~ m$VaccineTreatment)
ImpWIVModel <- lm(m$PrePostWIVImportance ~ m$WIVTreatment)
ImpRussiaModel <- lm(m$PrePostRussiaImportance ~ m$RussiaTreatment)


##Your Position


m$PrePostCubanPosition <- m$R_Cuban_Pos - m$Cuban_Pos
m$PrePostVaccinePosition <- m$R_Vaccine_Pos - m$Vaccine_Pos
m$PrePostWIVPosition <- m$R_WIV_Pos - m$WIV_Pos
m$PrePostRussiaPosition <- m$R_Russa_Pos - m$Russa_Pos

PosCubanModel <- lm(m$PrePostCubanPosition ~ m$CubanTreatment)
PosVaccineModel <- lm(m$PrePostVaccinePosition ~ m$VaccineTreatment)
PosWIVModel <- lm(m$PrePostWIVPosition ~ m$WIVTreatment)
PosRussiaModel <- lm(m$PrePostRussiaPosition ~ m$RussiaTreatment)


## RAW GROUP DIFFERENCES


m$DemsCubanPosition <- m$R_Cuban_OtherPos_Dems - m$Cuban_OtherPos_Dems
m$RepsCubanPosition <- m$R_Cuban_OtherPos_Reps - m$Cuban_OtherPos_Reps

m$DemsvsRepsCuban <- m$DemsCubanPosition - m$RepsCubanPosition

m$DemsVaccinePosition <- m$R_Vaccine_OtherPos_Dems - m$Vaccine_OtherPos_Dems
m$RepsVaccinePosition <- m$R_Vaccine_OtherPos_Reps - m$Vaccine_OtherPos_Reps

m$DemsvsRepsVaccine <- m$DemsVaccinePosition - m$RepsVaccinePosition


m$DemsWIVPosition <- m$R_WIV_OtherPos_Dems - m$WIV_OtherPos_Dems
m$RepsWIVPosition <- m$R_WIV_OtherPos_Reps - m$WIV_OtherPos_Reps

m$DemsvsRepsWIV <- m$DemsWIVPosition - m$RepsWIVPosition

m$DemsRussiaPosition <- m$R_Russia_OtherPos_Dems - m$Russia_OtherPos_Dems
m$RepsRussiaPosition <- m$R_Russia_OtherPos_Reps - m$Russia_OtherPos_Reps

m$DemVsRepsRussia <- m$DemsRussiaPosition - m$RepsRussiaPosition

## MODELS
DemVsRepsPosCubanModel <- lm(m$DemsvsRepsCuban ~ m$CubanTreatment)

DemVsRepsPosVaccineModel <- lm(m$DemsvsRepsVaccine ~ m$VaccineTreatment)

DemVsRepsPosWIVModel <- lm(m$DemsvsRepsWIV ~ m$WIVTreatment)

DemVsRepsPosRussiaModel <- lm(m$DemVsRepsRussia ~ m$RussiaTreatment)





#Polarization

## Cubans - Here I take people's perceptions of the other side and their side before treatment and get the difference
m$PreCubanPosMySide <- as.numeric(ifelse(m$Democrat == 1, m$Cuban_OtherPos_Dems, ifelse(m$Republican == 1, m$Cuban_OtherPos_Reps, "NA")))
m$PreCubanPosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$Cuban_OtherPos_Reps, ifelse(m$Republican == 1, m$Cuban_OtherPos_Dems, "NA")))

m$PreCubanSideDiff <- m$PreCubanPosMySide - m$PreCubanPosOtherSide

## Cubans - Here I take people's perceptions of the other side and their side after treatment and get the difference
m$PostCubanPosMySide <- as.numeric(ifelse(m$Democrat == 1, m$R_Cuban_OtherPos_Dems, ifelse(m$Republican == 1, m$R_Cuban_OtherPos_Reps, "NA")))
m$PostCubanPosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$R_Cuban_OtherPos_Reps, ifelse(m$Republican == 1, m$R_Cuban_OtherPos_Dems, "NA")))

m$PostCubanSideDiff <- m$PostCubanPosMySide - m$PostCubanPosOtherSide

## Cubans -Pulling it together
m$PrePostCubanSideDiff <- m$PostCubanSideDiff - m$PreCubanSideDiff



### VACCINE

## Vaccine - Here I take people's perceptions of the other side and their side before treatment and get the difference
m$PreVaccinePosMySide <- as.numeric(ifelse(m$Democrat == 1, m$Vaccine_OtherPos_Dems, ifelse(m$Republican == 1, m$Vaccine_OtherPos_Reps, "NA")))
m$PreVaccinePosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$Vaccine_OtherPos_Reps, ifelse(m$Republican == 1, m$Vaccine_OtherPos_Dems, "NA")))

m$PreVaccineSideDiff <- m$PreVaccinePosMySide - m$PreVaccinePosOtherSide

## Vaccine - Here I take people's perceptions of the other side and their side after treatment and get the difference
m$PostVaccinePosMySide <- as.numeric(ifelse(m$Democrat == 1, m$R_Vaccine_OtherPos_Dems, ifelse(m$Republican == 1, m$R_Vaccine_OtherPos_Reps, "NA")))
m$PostVaccinePosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$R_Vaccine_OtherPos_Reps, ifelse(m$Republican == 1, m$R_Vaccine_OtherPos_Dems, "NA")))

m$PostVaccineSideDiff <- m$PostVaccinePosMySide - m$PostVaccinePosOtherSide

## Vaccine -Pulling it together
m$PrePostVaccineSideDiff <- m$PostVaccineSideDiff - m$PreVaccineSideDiff

#### Covid Origins

## Covid Origins - Here I take people's perceptions of the other side and their side before treatment and get the difference
m$PreWIVPosMySide <- as.numeric(ifelse(m$Democrat == 1, m$WIV_OtherPos_Dems, ifelse(m$Republican == 1, m$WIV_OtherPos_Reps, "NA")))
m$PreWIVPosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$WIV_OtherPos_Reps, ifelse(m$Republican == 1, m$WIV_OtherPos_Dems, "NA")))

m$PreWIVSideDiff <- m$PreWIVPosMySide - m$PreWIVPosOtherSide

## Covid Origins - Here I take people's perceptions of the other side and their side after treatment and get the difference
m$PostWIVPosMySide <- as.numeric(ifelse(m$Democrat == 1, m$R_WIV_OtherPos_Dems, ifelse(m$Republican == 1, m$R_WIV_OtherPos_Reps, "NA")))
m$PostWIVPosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$R_WIV_OtherPos_Reps, ifelse(m$Republican == 1, m$R_WIV_OtherPos_Dems, "NA")))

m$PostWIVSideDiff <- m$PostWIVPosMySide - m$PostWIVPosOtherSide

## Covid Origins -Pulling it together
m$PrePostWIVSideDiff <- m$PostWIVSideDiff - m$PreWIVSideDiff

##### RUSSIA

## Russia - Here I take people's perceptions of the other side and their side before treatment and get the difference
m$PreRussiaPosMySide <- as.numeric(ifelse(m$Democrat == 1, m$Russia_OtherPos_Dems, ifelse(m$Republican == 1, m$Russia_OtherPos_Reps, "NA")))
m$PreRussiaPosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$Russia_OtherPos_Reps, ifelse(m$Republican == 1, m$Russia_OtherPos_Dems, "NA")))

m$PreRussiaSideDiff <- m$PreRussiaPosMySide - m$PreRussiaPosOtherSide

## Russia - Here I take people's perceptions of the other side and their side after treatment and get the difference
m$PostRussiaPosMySide <- as.numeric(ifelse(m$Democrat == 1, m$R_Russia_OtherPos_Dems, ifelse(m$Republican == 1, m$R_Russia_OtherPos_Reps, "NA")))
m$PostRussiaPosOtherSide <- as.numeric(ifelse(m$Democrat == 1, m$R_Russia_OtherPos_Reps, ifelse(m$Republican == 1, m$R_Russia_OtherPos_Dems, "NA")))

m$PostRussiaSideDiff <- m$PostRussiaPosMySide - m$PostRussiaPosOtherSide

## Russia -Pulling it together
m$PrePostRussiaSideDiff <- m$PostRussiaSideDiff - m$PreRussiaSideDiff



### Models

DiffModelCubans <- lm(PrePostCubanSideDiff ~ CubanTreatment, data = m)
summary(DiffModelCubans)

DiffModelVaccine <- lm(PrePostVaccineSideDiff ~ VaccineTreatment, data =m)
summary(DiffModelVaccine)

DiffModelWIV <- lm(PrePostWIVSideDiff ~ WIVTreatment, data =m)
summary(DiffModelWIV)

DiffModelRussia <- lm(PrePostRussiaSideDiff ~ RussiaTreatment, data =m)
summary(DiffModelRussia)

## RECODING FOR MAIN ANALYSIS:

### Importance
m$PrePostCubanImportance <- m$R_Cuban_Imp - m$Cuban_Imp
m$PrePostVaccineImportance <- m$R_Vaccine_Imp - m$Vaccine_Imp
m$PrePostWIVImportance <- m$R_WIV_Imp - m$WIV_Imp 
m$PrePostRussiaImportance <- m$R_Russia_Imp - m$Russia_Imp

### Position
m$PrePostCubanPosition <- m$R_Cuban_Pos - m$Cuban_Pos
m$PrePostVaccinePosition <- m$R_Vaccine_Pos - m$Vaccine_Pos
m$PrePostWIVPosition <- m$R_WIV_Pos - m$WIV_Pos
m$PrePostRussiaPosition <- m$R_Russa_Pos - m$Russa_Pos

### Polarization 
m$PreGapCubanPosition <- abs(m$Cuban_OtherPos_Dems - m$Cuban_OtherPos_Reps)
m$PostGapCubanPosition <- abs(m$R_Cuban_OtherPos_Dems - m$R_Cuban_OtherPos_Reps)

m$DemsvsRepsCuban <- m$PostGapCubanPosition - m$PreGapCubanPosition

m$PreGapVaccinePosition <- abs(m$Vaccine_OtherPos_Dems - m$Vaccine_OtherPos_Reps)
m$PostGapVaccinePosition <- abs(m$R_Vaccine_OtherPos_Dems - m$R_Vaccine_OtherPos_Reps)

m$DemsvsRepsVaccine <- abs(m$PostGapVaccinePosition - m$PreGapVaccinePosition)

m$PreGapWIVPosition <- abs(m$WIV_OtherPos_Dems - m$WIV_OtherPos_Reps)
m$PostGapWIVPosition <- abs(m$R_WIV_OtherPos_Dems - m$R_WIV_OtherPos_Reps)

m$DemsvsRepsWIV <- abs(m$PostGapWIVPosition - m$PreGapWIVPosition)

m$PreGapRussiaPosition <- abs(m$Russia_OtherPos_Dems - m$Russia_OtherPos_Reps)
m$PostGapRussiaPosition <- abs(m$R_Russia_OtherPos_Dems - m$R_Russia_OtherPos_Reps)

m$DemsvsRepsRussia <- m$PostGapRussiaPosition - m$PreGapRussiaPosition


#### Approval

m$PreDiffCubanApproval <- abs(m$Cuban_Approve_Dems - m$Cuban_Approve_Reps)
m$PostDiffCubanApproval <- abs(m$R_Cuban_Approve_Dems - m$R_Cuban_Approve_Reps)

m$DemVsRepsCubanApproval <- m$PostDiffCubanApproval - m$PreDiffCubanApproval

m$PreDiffVaccineApproval <- abs(m$Vaccine_Approve_Dems - m$Vaccine_Approve_Reps)
m$PostDiffVaccineApproval <- abs(m$R_Vaccine_Approve_Dems - m$R_Vaccine_Approve_Reps)

m$DemsVsRepsVaccineApproval <- m$PostDiffVaccineApproval - m$PreDiffVaccineApproval

m$PreDiffWIVApproval <- abs(m$WIV_Approve_Dems - m$WIV_Approve_Reps)
m$PostDiffWIVApproval <- abs(m$R_WIV_Approve_Dems - m$R_WIV_Approve_Reps)

m$DemsVsRepsWIVApproval <- m$PostDiffWIVApproval - m$PreDiffWIVApproval

m$PreDiffRussiaApproval <- abs(m$Russia_Approve_Dems - m$Russia_Approve_Reps)
m$PostDiffRussiaApproval <- abs(m$R_Russia_Approve_Dems - m$R_Russia_Approve_Reps)

m$DemsVsRepsRussiaApproval <- m$PostDiffRussiaApproval - m$PreDiffRussiaApproval

ApprovalCuban <- lm(m$DemVsRepsCubanApproval ~ m$CubanTreatment)
ApprovalVaccine <- lm(m$DemsVsRepsVaccineApproval ~ m$VaccineTreatment)
ApprovalWIV <- lm(m$DemsVsRepsWIVApproval ~ m$WIVTreatment)
ApprovalRussia <- lm(m$DemsVsRepsRussiaApproval ~ m$RussiaTreatment)


### KEY ANALYSIS:

y <- m
y <- dplyr::mutate(y, ID = row_number())

## Importance
y$ICuban <- y$PrePostCubanImportance
y$IRussia <- y$PrePostRussiaImportance
y$IVaccine <- y$PrePostVaccineImportance
y$IWIV <- y$PrePostWIVImportance


## Position
y$PCuban <- y$PrePostCubanPosition
y$PRussia <- y$PrePostRussiaPosition
y$PVaccine <- y$PrePostVaccinePosition
y$PWIV <- y$PrePostWIVPosition

## Gap Other Position 

y$GCuban <- m$DemsvsRepsCuban
y$GVaccine <- m$DemsvsRepsVaccine
y$GRussia <- m$DemsvsRepsRussia
y$GWIV <- m$DemsvsRepsWIV


## Gap APproval

y$ACuban <- m$DemVsRepsCubanApproval
y$AVaccine <- m$DemsVsRepsVaccineApproval
y$ARussia <- m$DemsVsRepsRussiaApproval
y$AWIV <- m$DemsVsRepsWIVApproval

y$Newsletter <- y$Version



longS <- subset(y, select = c('ID', "Democrat", "Republican", 'Independent', 'NonUS', "Newsletter", "OpenedEmail", 'ICuban', 'IRussia', 'IVaccine', 'IWIV', 'PCuban', 'PRussia', 'PVaccine', 'PWIV',  'GCuban', 'GRussia', 'GVaccine', 'GWIV',  'ACuban', 'ARussia', 'AVaccine', 'AWIV'))


long <- pivot_longer(longS, cols = -c('ID', "Democrat", "Republican", 'Independent',  'NonUS', "Newsletter","OpenedEmail"), names_to = c("Question" ,"Issue"), names_pattern = c("(.)(.*)") ,values_to = "ChangeInResponse" , values_drop_na = T)


## VERSION A = Cubans + Vaccine
## Version B = WIV + Russia

long$RepublicanInPartisanStoryReceived <- as.factor(ifelse(long$Republican == 1 & ( (long$Newsletter == "A" & long$Issue == "Cuban") | (long$Newsletter == "B" & long$Issue == "WIV") ) , 1, 0))
long$RepublicanOutPartisanStoryReceived <- as.factor(ifelse(long$Republican == 1 & ( (long$Newsletter == "A" & long$Issue == "Vaccine") | (long$Newsletter == "B" & long$Issue == "Russia") ) , 1, 0))


long$DemocratInPartisanStoryReceived <- as.factor(ifelse(long$Democrat == 1 &  (  (long$Newsletter == "A" & long$Issue == "Vaccine") | (long$Newsletter == "B" & long$Issue == "Russia") ) , 1, 0))
long$DemocratOutPartisanStoryReceived <- as.factor(ifelse(long$Democrat == 1  &  (  (long$Newsletter == "A" & long$Issue == "Cuban") | (long$Newsletter == "B" & long$Issue == "WIV") ) , 1, 0))

long$IndependentReceivedStory <- as.factor(ifelse(long$Independent == 1 &  (  (long$Newsletter == "A" & long$Issue == "Cuban") | (long$Newsletter == "A" & long$Issue == "Vaccine") | (long$Newsletter == "B" & long$Issue == "WIV") | (long$Newsletter == "B" & long$Issue == "Russia") ) , 1, 0))

long$NonUSRespondentReceivedStory <- as.factor(ifelse(long$NonUS == 1  & (  (long$Newsletter == "A" & long$Issue == "Cuban") | (long$Newsletter == "A" & long$Issue == "Vaccine") | (long$Newsletter == "B" & long$Issue == "WIV") | (long$Newsletter == "B" & long$Issue == "Russia") ) , 1, 0))


long$InPartisanStoryReceived <- as.factor(ifelse(long$RepublicanInPartisanStoryReceived == 1 | long$DemocratInPartisanStoryReceived == 1, 1, 0))
long$OutPartisanStoryReceived <- as.factor(ifelse(long$RepublicanOutPartisanStoryReceived == 1 | long$DemocratOutPartisanStoryReceived == 1, 1, 0))

long$TypeStoryReceived0 <- ifelse(long$RepublicanInPartisanStoryReceived == 1, "Republican received in-partisan", "No story")
long$TypeStoryReceived1 <- ifelse(long$RepublicanOutPartisanStoryReceived == 1, "Republican received Blindspot", long$TypeStoryReceived0)
long$TypeStoryReceived2 <- ifelse(long$DemocratInPartisanStoryReceived == 1, "Democrat received in-partisan", long$TypeStoryReceived1)
long$TypeStoryReceived3 <- ifelse(long$DemocratOutPartisanStoryReceived == 1, "Democrat received Blindspot", long$TypeStoryReceived2)
long$TypeStoryReceived4 <- ifelse(long$IndependentReceivedStory == 1, "Independent received story", long$TypeStoryReceived3)
long$TypeStoryReceived <- as.factor(ifelse(long$NonUSRespondentReceivedStory == 1, "Non-U.S. respondent received", long$TypeStoryReceived4))
long$TypeStoryReceived <- relevel(long$TypeStoryReceived, ref = "No story")


longG <- subset(long, long$Question == "G" )
longP <- subset(long, long$Question == "P" )
longI <- subset(long, long$Question == "I" )
longA <- subset(long, long$Question == "A" )

mod0c <- lm_robust(ChangeInResponse ~ TypeStoryReceived, clusters = ID, data = longG)
mod2c <- lm_robust(ChangeInResponse ~ TypeStoryReceived, clusters = ID, data = longP)
mod3c <-  lm_robust(ChangeInResponse ~ TypeStoryReceived, clusters = ID, data = longI)
mod4c <-  lm_robust(ChangeInResponse ~ TypeStoryReceived, clusters = ID, data = longA)

### MAKE TABLE 1 IN PAPER
library(texreg)

texreg(list(mod0c, mod2c, mod3c, mod4c), 
       cluster = list("ID", "ID", "ID", "ID"),
       #custom.coef.names = c("Intercept", "Repub-Good", "Rep-Bad", "Dem-Good", "Dem-Bad", "Independent", "Non-US"),
       digits = 2,
       stars = c(0.05, 0.01, 0.001),
       caption = "Regression Table Comparing Change in Response by Type of Story Received",
       label = "tab:multimodels",
       float.pos = "ht")

## ANALYSIS FOR CUBAN STORY  


longGCuban <- subset(longG, longG$Issue == "Cuban")
longGRussia <- subset(longG, longG$Issue == "Russia")
longGWIV <- subset(longG, longG$Issue == "WIV")
longGVaccine <- subset(longG, longG$Issue == "Vaccine")



longGopened <- subset(longG, longG$OpenedEmail == 1)
longPopened <- subset(longP, longP$OpenedEmail == 1)


modOpened0 <- lm(ChangeInResponse ~ TypeStoryReceived, data = longGopened)
modOpened1 <- lm(ChangeInResponse ~ TypeStoryReceived, data = longPopened)


modCubans <- lm(ChangeInResponse ~ TypeStoryReceived, data = longGCuban)


longGCubanOpened <- subset(longGCuban, longGCuban$OpenedEmail == 1)

modOpened0c <- lm(ChangeInResponse ~ TypeStoryReceived,  data = longGCubanOpened)

## TABLE FOR APPENDIX

texreg(modCubans, caption = "Change in issue polarization for Cuban issue")

texreg(modOpened0c, caption = "Change in issue polarization on Cuban issue among only those who Ground News confirms opened the newsletter")



longPCuban <- subset(longP, longP$Issue == "Cuban")
longPRussia <- subset(longP, longP$Issue == "Russia")
longPWIV <- subset(longP, longP$Issue == "WIV")
longPVaccine <- subset(longP, longP$Issue == "Vaccine")

longPCuban$ChangeInResponse <- longPCuban$ChangeInResponse / -1



modCubans2 <- lm(ChangeInResponse ~ TypeStoryReceived, data = longPCuban)



longPCubanOpened <- subset(longPCuban, longPCuban$OpenedEmail == 1)


modOpened1c <- lm(ChangeInResponse ~ TypeStoryReceived, data = longPCubanOpened)

## TABLES FOR APPENDIX


texreg(modCubans2, caption = "Change in people's issue positions for Cuban issue")

texreg(modOpened1c, caption = " Change in people's issue positions for Cuban issue among those who Ground News confirms to have opened the newsletter")

## MAKE FIGURE 2



library(ggpubr)
theme_set(theme_pubr())


p4 <- ggpredict(modCubans, "TypeStoryReceived") %>% plot(show.y.title = T, show.title=F)
p4 <- p4 + labs(x= "", y = "Predicted change", title = "Issue polarization - Cuban refugees") +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10"))  + theme(text = element_text(size=10, family="LM Roman 10")) + scale_y_continuous(limits = c(-.2, .05)) + geom_hline(yintercept =-0.03927  , color = "red") 
#ggsave(filename="p4.jpeg", plot=p4, device="jpeg", height=5, width=5, units="in", dpi=500)

p5 <- ggpredict(modCubans2, "TypeStoryReceived") %>% plot(show.y.title = T, show.title=F)
p5 <- p5 + labs(x= "Respondent partisan alignment", y = "Predicted change", title = "Favor the U.S. welcoming more Cuban refugees") +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10"))  + theme(text = element_text(size=10, family="LM Roman 10")) + scale_y_continuous(limits = c(-.15, .15)) + geom_hline(yintercept = -0.00672  , color = "red") 
#ggsave(filename="p5.jpeg", plot=p5, device="jpeg", height=5, width=5, units="in", dpi=500)

figure <- ggarrange(p4, p5,
                    labels = c("A", "B"),
                    ncol = 1, nrow = 2)
figure
#ggsave(filename="mainplot.jpeg", plot=figure, device="jpeg", height=5, width=5, units="in", dpi=500)




